package openGl;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import camera.OpenGl.utils.ByteBufferUtils;

import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.glClear;

/**
 * Created by Administrator on 2017/12/27.
 */

public class ShadowRender implements GLSurfaceView.Renderer {
    private String Tag="ShadowRender";
    /**
     * 顶点着色器
     */
    public  final String vertexShaderCode = "attribute vec4 a_Position;  \n" +
            "attribute vec4 a_Color;\n" +
            "\n" +
            "varying vec4 v_Color;\n" +
            "\n" +
            "void main()                    \n" +
            "{                            \n" +
            "    v_Color = a_Color;\n" +
            "\t  \n" +
            "    gl_Position = a_Position;    \n" +
            "    gl_PointSize = 10.0;          \n" +
            "}  ";
    /**
     * 片元着色器
     */
    public  final String fragmentShaderCode ="precision mediump float; \t\t\t\t\n" +
            "varying vec4 v_Color;      \t   \t\t\t\t\t\t\t\t\n" +
            "  \n" +
            "void main()                    \t\t\n" +
            "{                              \t\n" +
            "    gl_FragColor = v_Color;                                  \t\t\n" +
            "}";
    float[] tableVerticesWithTriangles = {
            // Order of coordinates: X, Y, R, G, B

            // Triangle Fan
            0f,    0f,   1f,   1f,   1f,
            -0.5f, -0.5f, 0.7f, 0.7f, 0.7f,
            0.5f, -0.5f, 0.7f, 0.7f, 0.7f,
            0.5f,  0.5f, 0.7f, 0.7f, 0.7f,
            -0.5f,  0.5f, 0.7f, 0.7f, 0.7f,
            -0.5f, -0.5f, 0.7f, 0.7f, 0.7f,

            // Line 1
            -0.5f, 0f, 1f, 0f, 0f,
            0.5f, 0f, 1f, 0f, 0f,

            // Mallets
            0f, -0.25f, 0f, 0f, 1f,
            0f,  0.25f, 1f, 0f, 0f
    };
    private FloatBuffer trianglesBuffer;

    public ShadowRender() {
        trianglesBuffer= ByteBufferUtils.makeFloteByteBuffer(tableVerticesWithTriangles);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glClearColor(0,0,0,0);
        int program = GLES20.glCreateProgram();
        int i = OpenGlUtils.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int i1 = OpenGlUtils.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
        GLES20.glAttachShader(program,i);
        GLES20.glAttachShader(program,i1);
        GLES20.glLinkProgram(program);
        
        GLES20.glUseProgram(program);
        int a_position = GLES20.glGetAttribLocation(program, "a_Position");
        int a_color = GLES20.glGetAttribLocation(program, "a_Color");
        trianglesBuffer.position(0);
        GLES20.glEnableVertexAttribArray(a_position);
        GLES20.glVertexAttribPointer(
                a_position,
                2,
                GLES20.GL_FLOAT,
                false,
                5*4,//步长
                trianglesBuffer
                );
        trianglesBuffer.position(2);
        GLES20.glEnableVertexAttribArray(a_color);
        GLES20.glVertexAttribPointer(a_color,3,GLES20.GL_FLOAT,
                false,5*4,trianglesBuffer);
        
        

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0,0,width,height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        glClear(GL_COLOR_BUFFER_BIT);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN,0,6);
        GLES20.glDrawArrays(GLES20.GL_LINES,6,2);
    }
}
